package com.daoeping.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class TimeDiff {
	public String getTimeDiff(String createTime , String updateTime){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String tempTime = "";
		if(updateTime!=null&&!updateTime.equals("null")){
			tempTime = updateTime;
		}else{
			tempTime = createTime;
		}
		try {
			Date beginTime = df.parse(tempTime);
			Date endTime = df.parse(new Timestamp(System.currentTimeMillis()).toString().substring(0, 19));
			long l=endTime.getTime()-beginTime.getTime(); 
			if(l==0){
				tempTime = "1秒";
			}else {
				long day = l/(24*60*60*1000);   
				long hour = (l/(60*60*1000)-day*24);   
				long minute = ((l/(60*1000))-day*24*60-hour*60);   
				long second = (l/1000-day*24*60*60-hour*60*60-minute*60);   
				if(day!=0){
					tempTime = day + "天前";
				}else if(hour!= 0){
					tempTime = hour + "小时前";
				}else if(minute != 0){
					tempTime = minute + "分钟前";
				}else if(second != 0){
					tempTime = second + "秒";
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tempTime;
	}
	public static void main(String[] args) {
		System.out.println(new TimeDiff().getTimeDiff(null , "2013-01-02 12:20:20" ));
	}
	
	
}
